<template>
  <div class="bar-chart-18 chart" ref="barChart18"></div>
</template>

<script setup>
import { ref, onMounted, inject } from "vue";
const barChart18 = ref(null);
const echarts = inject("$echarts");

// 参数
const xAxisData = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'];
const seriesData = [20125, 12306, 37900, 23494, 16546, 12302, 37901, 23495, 16548, 37906, 23499, 16540];

const CubeLeft = echarts.graphic.extendShape({
  shape: {
    x: 0,
    y: 0,
  },
  buildPath: function (ctx, shape) {
    const xAxisPoint = shape.xAxisPoint;
    const c0 = [shape.x, shape.y];
    const c1 = [shape.x - 9, shape.y - 9];
    const c2 = [xAxisPoint[0] - 9, xAxisPoint[1] - 9];
    const c3 = [xAxisPoint[0], xAxisPoint[1]];
    ctx.moveTo(c0[0], c0[1]).lineTo(c1[0], c1[1]).lineTo(c2[0], c2[1]).lineTo(c3[0], c3[1]).closePath();
  },
});
const CubeRight = echarts.graphic.extendShape({
  shape: {
    x: 0,
    y: 0,
  },
  buildPath: function (ctx, shape) {
    const xAxisPoint = shape.xAxisPoint;
    const c1 = [shape.x, shape.y];
    const c2 = [xAxisPoint[0], xAxisPoint[1]];
    const c3 = [xAxisPoint[0] + 18, xAxisPoint[1] - 9];
    const c4 = [shape.x + 18, shape.y - 9];
    ctx.moveTo(c1[0], c1[1]).lineTo(c2[0], c2[1]).lineTo(c3[0], c3[1]).lineTo(c4[0], c4[1]).closePath();
  },
});
const CubeTop = echarts.graphic.extendShape({
  shape: {
    x: 0,
    y: 0,
  },
  buildPath: function (ctx, shape) {
    const c1 = [shape.x, shape.y];
    const c2 = [shape.x + 18, shape.y - 9];
    const c3 = [shape.x + 9, shape.y - 18];
    const c4 = [shape.x - 9, shape.y - 9];
    ctx.moveTo(c1[0], c1[1]).lineTo(c2[0], c2[1]).lineTo(c3[0], c3[1]).lineTo(c4[0], c4[1]).closePath();
  },
});
echarts.graphic.registerShape('CubeLeft', CubeLeft);
echarts.graphic.registerShape('CubeRight', CubeRight);
echarts.graphic.registerShape('CubeTop', CubeTop);
const option = {
  compCode: "C202309030",
  backgroundColor: "#012366",
  grid: {
    left: '0%',
    right: 0,
    bottom: '5%',
    top: '5%',
    containLabel: true,
  },
  tooltip: {
    trigger: 'axis',
    backgroundColor: '#053962', // 设置背景色为半透明红色
    borderColor: '#165e9e', // 设置边框颜色
    borderWidth: 1,
    textStyle: {
      color: '#fff'
    },
    // 坐标轴指示器，坐标轴触发有效(axis)
    axisPointer: {
      type: 'shadow', // 默认为直线，可选为：'line' | 'shadow'
    },
    formatter: function (params) {
      // console.log(params, 'params');
      let returnData = '';
      for (let i = 0; i < params.length; i++) {
        let indexColor = '#da953e';
        returnData =
          '<span style="display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background:' +
          indexColor +
          '"></span>';
        returnData += params[i].name + '<br/><span style="padding-left:13px">' + params[i].value + '</span>';
      }
      return returnData;
    },
    extraCssText: 'font-size: 22px; line-height: 30px'
  },
  xAxis: {
    type: 'category',
    data: xAxisData,
    axisLine: {
      show: true,
      lineStyle: {
        color: '#b1b19c',
      },
    },
    offset: 10,
    axisTick: {
      show: false,
      length: 9,
      alignWithLabel: true,
      lineStyle: {
        color: '#fff',
      },
    },
    axisLabel: {
      fontSize: 16,
      color: '#fff'
    },
  },
  yAxis: {
    type: 'value',
    axisLine: {
      show: true,
      lineStyle: {
        color: '#b1b19c',
      },
    },
    splitLine: {
      show: false,
    },
    axisTick: {
      show: false,
    },
    axisLabel: {
      fontSize: 16,
      color: '#fff'
    },
    boundaryGap: ['20%', '20%'],
  },
  series: [
    {
      type: 'custom',
      renderItem: (params, api) => {
        const location = api.coord([api.value(0), api.value(1)]);
        return {
          type: 'group',
          children: [
            {
              type: 'CubeLeft',
              shape: {
                api,
                xValue: api.value(0),
                yValue: api.value(1),
                x: location[0],
                y: location[1],
                xAxisPoint: api.coord([api.value(0), 0]),
              },
              style: {
                fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: '#d5ac37',
                  },
                  {
                    offset: 1,
                    color: '#df8244',
                  },
                ]),
              },
            },
            {
              type: 'CubeRight',
              shape: {
                api,
                xValue: api.value(0),
                yValue: api.value(1),
                x: location[0],
                y: location[1],
                xAxisPoint: api.coord([api.value(0), 0]),
              },
              style: {
                fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: '#d5ac37',
                  },
                  {
                    offset: 1,
                    color: '#df8244',
                  },
                ]),
              },
            },
            {
              type: 'CubeTop',
              shape: {
                api,
                xValue: api.value(0),
                yValue: api.value(1),
                x: location[0],
                y: location[1],
                xAxisPoint: api.coord([api.value(0), 0]),
              },
              style: {
                fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: '#d5ac37',
                  },
                  {
                    offset: 1,
                    color: '#df8244',
                  },
                ]),
              },
            },
          ],
        };
      },
      data: seriesData,
    },
    {
      type: 'bar',
      label: {
        normal: {
          show: true,
          position: 'top',
          fontSize: 14,
          color: '#fff',
          offset: [2, -25],
        },
      },
      itemStyle: {
        color: 'transparent',
      },
      tooltip: {
        backgroundColor: '#053962', // 设置背景色为半透明红色
        borderColor: '#165e9e', // 设置边框颜色
        borderWidth: 1,
        textStyle: {
          color: '#fff'
        },
      },
      data: seriesData,
    },
  ],
  animationDuration: 3000,
  animationEasing: 'cubicInOut',
};

onMounted(() => {
  const chart = echarts.init(barChart18.value);
  chart.setOption(option);
});
</script>

<style scoped></style>
